package name.vaccari.matteo.tai.phonedirectory.network;

import static org.junit.Assert.assertEquals;

import java.io.Reader;
import java.io.StringReader;

import org.junit.Test;

public class HttpRequestTest {

	@Test
	public void testParsePost() throws Exception {
		String text = "POST /foobar HTTP/1.1\r\n" 
			+ "Host: localhost:4444\r\n"
			+ "User-Agent: Mozilla/5.0\r\n" 
			+ "\r\n" + "aaa=xxx&bbb=yyy";

		Reader reader = new StringReader(text);
		HttpRequest request = new HttpRequest(reader);
		assertEquals("POST", request.getMethod());
		assertEquals("/foobar", request.getPath());
		assertEquals("localhost:4444", request.getHeader("host"));
		assertEquals("aaa=xxx&bbb=yyy", request.getBody());
		assertEquals("xxx", request.getParams().get("aaa"));
		assertEquals("yyy", request.getParams().get("bbb"));
		assertEquals(null, request.getParams().get("lkjhlkd"));
	}
	
	@Test
	public void testParseGet() throws Exception {
		String text = "GET /pippo/pluto HTTP/1.1\r\n" 
			+ "Host: localhost:4444\r\n"
			+ "User-Agent: Mozilla/5.0\r\n" 
			+ "\r\n"
			;

		Reader reader = new StringReader(text);
		HttpRequest request = new HttpRequest(reader);
		assertEquals("GET", request.getMethod());
		assertEquals("/pippo/pluto", request.getPath());
		assertEquals("localhost:4444", request.getHeader("host"));
		assertEquals("", request.getBody());
		assertEquals(null, request.getParams().get("lkjhlkd"));
	}

}
